package com.cws4cloud.cws.mall.xsm.util;

import org.apache.commons.codec.binary.Base64;

import java.io.UnsupportedEncodingException;

public class EncryptDecrypt {

	/**
	 * 加密 
	 * @param content 需要加密的内容
	 * @param password  解密密钥
	 * @return
	 */
	public static String encrypt(String content, String password) {
		String rtnStr = "";
		byte[] rltByte = AES.encrypt_aes(content, password);
		if(rltByte == null){
			throw new RuntimeException("encrpte exception!check console log to get more information!");
		}else{
			String hexStr = parseByte2HexStr(rltByte);
			rtnStr = new String(Base64.encodeBase64(hexStr.getBytes()));
		}
		return rtnStr;
	}

	/**
	 * 解密 
	 * @param content  待解密内容 
	 * @param password 解密密钥 
	 * @return 
	 */
	public static String decrypt(String content, String password) {
		String rtnStr = "";
		
		byte[] contentByte = Base64.decodeBase64(content.getBytes());
		byte[] aesByte = parseHexStr2Byte(new String(contentByte));
		
		byte[] rltByte = AES.decrypt_aes(aesByte, password);
		if(rltByte == null){
			throw new RuntimeException("decrpte exception!check console log to get more information!");
		}else{
			try {
				rtnStr = new String(rltByte,"UTF8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
		}
		return rtnStr;
	}


	/**将二进制转换成16进制 
	 * @param buf 
	 * @return 
	 */   
	public static String parseByte2HexStr(byte buf[]) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < buf.length; i++) {
			String hex = Integer.toHexString(buf[i] & 0xFF);
			if (hex.length() == 1) {
				hex = '0' + hex;   
			}
			sb.append(hex.toUpperCase());
		}
		return sb.toString();
	}

	/**将16进制转换为二进制 
	 * @param hexStr 
	 * @return 
	 */   
	public static byte[] parseHexStr2Byte(String hexStr) {
		if (hexStr.length() < 1)
			return null;
		byte[] result = new byte[hexStr.length()/2];
		for (int i = 0;i< hexStr.length()/2; i++) {
			int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16);
			int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16);
			result[i] = (byte) (high * 16 + low);   
		}
		return result;   
	}

//	public static void main(String[] args) throws UnsupportedEncodingException {
//		long start = System.currentTimeMillis();
//		String content = "{\"bill_acct\":\"17000\",\"currType\":\"156\",\"order_date\":\"20150911\",\"from_order_id\":\"0000003579P388\",\"exp_date\":\"20150911233500\",\"custName\":\"卫兵综合商店\",\"ref_id\":\"610204191553\"}";   
//		String password = "6A198E59AF3DE87507A6E95D0620EAA56A198E59AF3DE87507A6E95D0620EAA56A198E59AF3DE87507A6E95D0620EAA56A198E59AF3DE87507A6E95D0620EAA5";   
//		//加密
//		System.out.println("加密前：" + content);
//		String encryptResult = encrypt(content, password);
//		
//		System.out.println("加密后：" + encryptResult);
//		
//		encryptResult = "NUUzRDc3RjQyNkEwNTFFRDAzNkQ5MkJFRUVDNzNGOTA2M0I1RTAzMjIzMTZFQ0VDQkM5RDU4N0RDM0Q2M0U2OTc1QUJENjE4NzlCQUFGRUZCRDQxQ0VFMkVFNUJCRTRCN0VGRkE5QjdCOTVBMkY3QjNGNjZEQTYwMzA4NkZGQTlCNEUzQTAwQUQwNEU3RUQyRDQ0NThCNDFCRTcyQUYwNEJENjFGQTcyMUJFQUU4RUMyRUQ5NjdDRDZCMzU1OEU2M0EyRjkzNjVEQkZCRENCOEVFMzAwOUMzREVGOURBRkFBRUZGRTcxOTBGOEUyMzJCOTExOEI5NDRENTVGMTU5RjY2OTc3QkU5NjdBNjhFRDdBRjRCRjJFMTA5NkIxQjQ5NUZERjRBQTFEQkQ0OTg4RDk0ODkwRTU1RkVERTA0MDdBRDIzMDUwMjBBQThDMzIyMEUzRjhFRkZFNTU4MzdFQzlFMEU2ODE0NUE3QUE4NjFGM0RBRTBDNzJERUZBQ0VE";
//		//解密
//		String decryptResult = decrypt(encryptResult,password);
//		System.out.println("解密后：" + decryptResult);
//		
//		System.out.println("cost time:"+(System.currentTimeMillis()-start));
//	}
}
